package birdsVSZombiesServer;

import processing.core.PApplet;
import processing.net.Client;
import processing.net.Server;
import shared.BVZClientToServerMessages;
import shared.BVZConstants;

/**
 * The thread that listens to the mouse information from different clients.
 * @author Yang Liu
 */
public class BVZMouseListener extends Thread {
	
	public static BirdsVSZombiesServerThread parent;
	
	public BVZMouseListener(){
		super();
	}
	
	public void run(){
		while(true){
			Client bvzClient = parent.bvzServer.available();
			if(bvzClient != null){
				String dataIn = bvzClient.readStringUntil('*');
				if(dataIn != null){
					dataIn = dataIn.substring(0, dataIn.length() -1);
					parent.incomingMessage = parent.gson.fromJson(dataIn, BVZClientToServerMessages.class);
					
//					println("Client says: " + dataIn);
					
					switch (parent.incomingMessage.clientStat) {
						//	For debug server
						case BVZConstants.CLIENTSTART:
							parent.makeBVZPlayer(bvzClient, 0);
							parent.makeBVZPlayer(bvzClient, 1);
							parent.println("Clients: "+ bvzClient.ip() +" started!!!");
							//	A test for server phys simulation
							parent.callRunASimplePhys();
							
							break;
						case BVZConstants.CLIENTJOININ:
							parent.makeBVZPlayer(bvzClient, parent.incomingMessage.playerRole);
							if(parent.bvzPlayerNum == 2)
								parent.callRunASimplePhys();
							break;
						case BVZConstants.CLIENTRUNNINGPHYS:
							switch (parent.incomingMessage.mouseStat) {
								case BVZConstants.MOUSEPRESSED:
									parent.createMouseJoint();
									break;
								case BVZConstants.MOUSEDRAGGED:
									parent.updateMouseJoint();
									break;
								case BVZConstants.MOUSERELEASED:
									parent.destoryMouseJoint();
									break;
								default:
									break;
							}
							
							break;
						default:
							break;
					}
				}
				else{
//					parent.println("No data Recieved!!!");
				}
			}
		}
	}
}
